package com.kumi.crowd.controller;

import com.kumi.crowd.bean.Role;
import com.kumi.crowd.service.AdminService;
import com.kumi.crowd.service.AuthService;
import com.kumi.crowd.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/assignRole")
public class assignRoleController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthService authService;

    @RequestMapping("/page")
    public String inner(@RequestParam("id") Integer adminId, ModelMap modelMap){
        List<Role> roleList = roleService.getAssignedRole(adminId);

        List<Role> unRoleList = roleService.getUnAssignedRole(adminId);

        modelMap.addAttribute("roleList",roleList);
        modelMap.addAttribute("unRoleList",unRoleList);

        return "assignRole";
    }

    @RequestMapping("/add")
    public String add(@RequestParam("id") Integer id,
                      @RequestParam("pageNum") Integer pageNum,
                      @RequestParam("keyword") String keyword,
                      @RequestParam(value = "roleId",required = false) List<Integer> roleId){

        adminService.addRoleAndAdmin(id,roleId);
        return "redirect:/admin/pageInfo?pageNum=" + pageNum + "&keyword=" + keyword;
    }
}
